<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel='stylesheet' href='../rurple.css' type='text/css'>
<title>For a while</title>
</head>
<body>
<h2 class="title">16. For a <span class="pykeyword">while</span> ...</h2>
<p>When we want to repeat some instructions until a certain condition is
satisfied, Python gives us a simpler way to write this using a new keyword:
<span class="pykeyword">while</span>. Let me show you first how this would
look using <i>pseudocode</i> to rewrite an example we have seen before.<br>
<br>
While not on beeper,<br>
... keep moving;<br>
otherwise,<br>
... stop.<br>
<br>
You should agree that this expresses the same idea as before. Using Python
code, here how we would actually write it:</p>
<pre>
<span class="keyword">while not</span> on_beeper():
    move()
 
turn_off()           
</pre>
<p>No more need to <tt>repeat</tt>. <b>Try it!</b></p>
<hr width="50%">
<h3 class="try">Your turn</h3>
<p>Use <span class="pykeyword">while</span> and <span class=
"pykeyword">not</span> to rewrite the jumping hurdle program so that you
don't have to use an arbitrary number of repeating statements. In other
words, the core of your program should look like the following:</p>
<pre>
<span class="keyword">while not</span> next_to_a_beeper():
    move_or_jump()
 
turn_off()           
</pre>
<p>Make sure it works!</p>
<h3 class="suggested">Weeding and seeding time</h3>
<p>It is spring time again. Reeborg's father has seeded the garden for the
fall harvest. Just like last time, in some places, two seeds have sprouted
whereas in others none have. A typical situation is shown below (file:
harvest4.wld).</p>
<p><img alt="weeding" src="../../images/intro/harvest4.png"></p>
<p>Help Reeborg weed the garden, so that there are no places with two carrots
(beepers), and reseed so that there are no places with none.</p>
<p>Here's a <i>suggestion</i> for part of the code using the new keyword
<span class="pykeyword">while</span>:</p>
<pre>
<span class="comment"># introducing vocabulary related to the problem</span>
next_to_a_carrot = on_beeper
plant_carrot = put_beeper
pick_carrot = pick_beeper

<span class="keyword">def</span> one_carrot_only():
    <span class="keyword">while</span> next_to_a_carrot():
        pick_carrot()      <span class="comment"># pick them all!</span>            
    plant_carrot()         <span class="comment"># replant only one!</span>
</pre>
<p>This piece of code is quite a bit shorter than the previous one (three
lines inside the <span class="pykeyword">def</span>inition instead of six).
Furthermore, it can work even when more than two seeds have sprouted at the
same spot! <b>Try it!</b><br>
<small>Remember that, in reality, it is <b>not</b> a good idea to remove
seedlings and replant them right away!</small></p>
<center><a href="15-not.htm"><img alt="previous" src=
"../../images/previous.png"> Not true!?</a> - 
<a href="../lessons_toc.htm"><img alt=
"home" src="../../images/home.png"></a> - <a href="17-amazing1.htm">Amazing -
part 1 <img alt="next" src="../../images/next.png"></a></center>
</body>
</html>
